<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();%>

<div class="callout callout-left">
  <span class="callout-title text-muted">软件激活统计</span>
  <a class="btn btn-primary"   href="javascript:;" id="export_excel" style="margin-left: 35px;"><span class="glyphicon glyphicon-export">&nbsp;导出excel</span></a>
</div>

<div class="callout">
  <form class="form-inline"  >
  <div class="form-group">
    <label for="softName_search">软件名称：</label>
    <select class="form-control" id="softName_search" name="softName_search">
    	<option value="">请选择</option>
    </select>
  </div>
  <div class="form-group">
    <label for="start_search">开始日期：</label>
    <div class='input-group'>
        <input type='text' class='form-control' id='start_search' name='start_search' placeholder='开始日期'/>
        <span class='glyphicon glyphicon-calendar input-group-addon' style='top: 0;'></span>
    </div>
  </div>
  <div class="form-group">
    <label for="end_search">结束日期：</label>
    <div class='input-group'>
      <input type='text' class='form-control' id='end_search' name='start_search' placeholder='结束日期'/>
      <span class='glyphicon glyphicon-calendar input-group-addon' style='top: 0;'></span>
    </div>
  </div>&nbsp;
  <button type="button" class="btn btn-primary" id="doSearch">
    <span class="glyphicon glyphicon-search">&nbsp;搜索</span>
  </button>
</form>
</div>

<div class="callout callout-white">
<div class="table-responsive center-block" style="max-width: 550px;">
  <table class="table table-bordered" id="t_excel">
    <thead>
      <tr>
        <th>#</th>
        <th>软件名称</th>
      	<th>激活成功</th>
        <th>激活失败</th>
      </tr>
    </thead>
    <tbody id="active_content">
      
    </tbody>
  </table>
  </div>
</div>

<script>
require.config({
	paths: {
		jquery: 'jquery-1.9.1.min',
		commmon: 'common',
		bootstrapswitch: 'bootstrap-switch.min'
	}
});
require(['jquery', 'common', 'bootstrapswitch'], function($, common) {
//console.log("软件激活情况模块载入");

var workspace = function() {

	var dataOpt;
	var recordAmounts;
	var softwares;

	var init = function() {
		dataOpt = common.dataOpt;
		softwares = common.ajaxLoadData(dataOpt.softwareOpt);

		_datePiclerInit();
		_initTable();
		_doSearch();
		common.exportExcel("软件激活情况统计");
	}

	// 日期控件
  var _datePiclerInit = function() {
    [$("#start_search"), $("#end_search")].forEach(function(el) {
        $(el).val(new Date().Format("yyyy-MM-dd"));
        common.datePickerInit(el);
    })
  }

	// 初始化表格
	var _initTable = function() {
		recordAmounts = common.ajaxLoadData(dataOpt.recordAmountOpt);
		dataOpt.recordAmountOpt.data = {}
		
		var template = "<tr><th>#row#</th><td>#softsName#</td><td>#success#</td><td>#failure#</td></tr>";

		var row = 1;
		var html = "";

		for(var record in recordAmounts) {
			var thisT = template;
			thisT = thisT.replace(/#row#/g, row++).replace(/#softsName#/g, record)
				.replace(/#success#/g, recordAmounts[record]["激活成功"] ? recordAmounts[record]["激活成功"] : 0)
				.replace(/#failure#/g, recordAmounts[record]["激活错误"] ? recordAmounts[record]["激活错误"] : 0);
			html += thisT;
		}
		$("#active_content").html(html);
	}
	
	// 搜索
	var _doSearch = function() {
		// 软件名称
		softwares.forEach(function(software) {
			var opt = document.createElement("option");
			$(opt).val(software["softName"]);
			$(opt).text(software["softName"]);
			$("#softName_search").append(opt);
		});

		$("#doSearch").click(function() {
      var startDate = $("#start_search").val();
      var endDate = $("#end_search").val();

      if(startDate === endDate) {
        var addDate = new Date(endDate);
        addDate.setDate(addDate.getDate() + 1);
        endDate = addDate.Format("yyyy-MM-dd");
      }

			dataOpt.recordAmountOpt.data = {
				"softName": $("#softName_search").val(),
        "startDate": startDate,
        "endDate": endDate
			}
			_initTable();
		});
	}

	return {
		init: init
	}
}();
	
$(function() { workspace.init(); });
});

requirejs.onError = function (err) {  
  console.error(err);
  if (err.requireType === 'timeout') {
      console.error('modules: ' + err.requireModules);
  }
  throw err;
}
</script>